<?php

namespace App\Jobs;

use App\Models\SeckillGood;
use App\Models\SeckillOrderModel;
use Godruoyi\Snowflake\Sonyflake;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class MakeOrdeJob implements ShouldQueue
{
	use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
	
	/**
	 * Create a new job instance.
	 *
	 * @return void
	 */
	public $userId;
	public $seckillId;
	
	public function __construct($userId, $seckillId)
	{
		$this->userId = $userId;
		$this->seckillId = $seckillId;
	}
	
	/**
	 * Execute the job.
	 *
	 * @return void
	 */
	public function handle()
	{
		//订单号雪花
		$snow = new Sonyflake();
		$order_number = $snow->id();
		//用户id
		$user_id = $this->userId;
		//秒杀商品id
		$seckillgoods_id = $this->seckillId;
		//秒杀商品
		$con = (new SeckillGood())->where('goods_id', $seckillgoods_id)->first();
		//实际支付价格
		$seckill_price = $con->seckill_price;
		//支付成功时间
		$pay_time = date('Y-m-d H:i:s');
		//支付状态 0待支付1支付成功2支付过期
		$pay_state = 0;
		//存入订单表
		$param = [
			'order_number' => $order_number,
			'user_id' => $user_id,
			'seckillgoods_id' => $seckillgoods_id,
			'seckill_price' => $seckill_price,
			'pay_time' => $pay_time,
			'pay_state' => $pay_state,
		];
		
		(new SeckillOrderModel())->create($param);
		//立马执行延时队列，对支付状态作判断,订单回收，未支付的改状态 回队列
		//生成订单之后，rabbitmq延时队列 订单号
		NoPayOrderJob::dispatch($order_number, $seckillgoods_id)->delay(now()->addMinutes(1));
		
	}
}
